package com.qgzj.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qgzj.common.utils.RandomIDUtils;
import com.qgzj.common.vo.MaterialRequest;
import com.qgzj.entity.TeachingMaterial;
import com.qgzj.mapper.TeachingMaterialMapper;
import com.qgzj.response.PageQueryRes;
import com.qgzj.util.ApplicationUtil;
import com.qgzj.util.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class TeachingMaterialService {

    @Resource
    TeachingMaterialMapper materialMapper;

    public PageQueryRes<TeachingMaterial> selectMaterial(MaterialRequest materialReq) {
        int page = materialReq.getPage();
        int pageSize = materialReq.getPageSize();
        PageQueryRes res = new PageQueryRes();
        res.setPage(page);
        res.setPageSize(pageSize);

        EntityWrapper<TeachingMaterial> wrapper = new EntityWrapper<>();
        if (StringUtils.isNotBlank(materialReq.getMaterialName())) {
            wrapper.eq("material_name", materialReq.getMaterialName());
        }
        if (StringUtils.isNotBlank(materialReq.getStage())) {
            wrapper.eq("stage", materialReq.getStage());
        }
        if (StringUtils.isNotBlank(materialReq.getStatusInfo())) {
            wrapper.eq("status_info", materialReq.getStatusInfo());
        }
        wrapper.orderBy("id",false);

        List<TeachingMaterial> materials = materialMapper.selectPage(new Page<>(page, pageSize), wrapper);

        res.setCount(materialMapper.selectCount(wrapper));
        res.setList(materials);
        return res;
    }

    public void insertMaterial(MaterialRequest materialReq) {
        TeachingMaterial material = transMaterialReqToMaterial(materialReq);
        material.setCreateTime(new Date());
        material.setUpdateTime(new Date());
        if (StringUtils.isBlank(materialReq.getMaterialNo())) {
            material.setMaterialNo(ApplicationUtil.getCommonNo());
        }
        materialMapper.insert(material);
    }

    public void updateMaterial(MaterialRequest materialReq) {
        TeachingMaterial material = transMaterialReqToMaterial(materialReq);
        material.setUpdateTime(new Date());
        String materialNo = material.getMaterialNo();
        EntityWrapper<TeachingMaterial> wrapper = new EntityWrapper<>();
        wrapper.eq("material_no", materialNo);
        Integer update = materialMapper.update(material, wrapper);
    }

    public void deleteMaterial(MaterialRequest materialReq) {
        String materialNo = materialReq.getMaterialNo();
        EntityWrapper<TeachingMaterial> wrapper = new EntityWrapper<>();
        wrapper.eq("material_no", materialNo);
        Integer update = materialMapper.delete(wrapper);
    }

    public TeachingMaterial transMaterialReqToMaterial(MaterialRequest materialReq) {
        TeachingMaterial material = new TeachingMaterial();
//        if (StringUtils.isNotBlank(materialReq.getMaterialName())) {
//            material.setMaterialName(materialReq.getMaterialName());
//        }
//        if (StringUtils.isNotBlank(materialReq.getMaterialNo())) {
//            material.setMaterialNo(materialReq.getMaterialNo());
//        }
//        if (StringUtils.isNotBlank(materialReq.getStage())) {
//            material.setStage(materialReq.getStage());
//        }
//        //描述
//        if (StringUtils.isNotBlank(materialReq.getCourseIntroduction())) {
//            material.setCourseIntroduction(materialReq.getCourseIntroduction());
//        }
//        if (StringUtils.isNotBlank(materialReq.getCoverPicUrl())) {
//            material.setCoverPicUrl(materialReq.getCoverPicUrl());
//        }
//        if (null != materialReq.getVolumeNum()) {
//            material.setVolumeNum(materialReq.getVolumeNum());
//        } else {
//            material.setVolumeNum(1);
//        }
//        if (null != materialReq.getPageNum()) {
//            material.setPageNum(materialReq.getPageNum());
//        } else {
//            material.setPageNum(1);
//        }
//        if (StringUtils.isNotBlank(materialReq.getPublishingHouse())) {
//            material.setPublishingHouse(materialReq.getPublishingHouse());
//        }
//        if (null != materialReq.getOriginalPrice()) {
//            material.setOriginalPrice(materialReq.getOriginalPrice());
//        } else {
//            material.setOriginalPrice(new BigDecimal(0));
//        }
//        if (null != materialReq.getCurrentPrice()) {
//            material.setCurrentPrice(materialReq.getCurrentPrice());
//        } else {
//            material.setCurrentPrice(new BigDecimal(0));
//        }
//        if (null != materialReq.getCreateTime()) {
//            material.setCreateTime(materialReq.getCreateTime());
//        }
//        if (null != materialReq.getUpdateTime()) {
//            material.setUpdateTime(materialReq.getUpdateTime());
//        }

        BeanUtils.copyProperties(materialReq, material);
        return material;
    }


}
